using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Impact.Model.Objects;
using Impact.Web.Foundation;

namespace Impact.Web.Controls
{
    public partial class CommentListing : Impact.Web.Foundation.UserControlBase
    {

        /// <summary>
        /// performs any extra startup processing
        /// </summary>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (article != null)
            {
                commentsRepeater.DataSource = article.Comments;
                commentsRepeater.DataBind();

                editor.ArticleId = article.ArticleId;
                editor.Visible = article.AllowComments;
            }

        }

        private Article article;

        /// <summary>
        /// Gets or Sets the article that this control will display comments for
        /// </summary>
        public Article Article
        {
            get { return article; }
            set { article = value; }
        }

        /// <summary>
        /// given a ArticleComment object, this method formats the author's name with
        /// a URL if it is provided
        /// </summary>
        protected string FormatAuthorLink(object dataItem)
        {
            ArticleComment comment = dataItem as ArticleComment;
            string output;

            if (string.IsNullOrEmpty(comment.AuthorUrl))
            {
                output = string.Format("{0} - {1}", comment.Author, comment.CreatedOn.ToString("MMM dd, yyyy HH:mm"));
            }
            else
            {
                output = string.Format("<a href=\"{0}\" target=\"_blank\">{1}</a> - {2}",
                    comment.AuthorUrl, comment.Author, comment.CreatedOn.ToString("MMM dd, yyyy HH:mm"));
            }

            return output;
        }


    }
}